
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 06/17/2010 11:00:52
-- Generated from EDMX file: D:\Projekty\Bazy Danych\ms-mini-wrs\src\WRSManagementSystem.DataAccess\WRSEntities.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [WRSDatabase];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_PersonFunction_Person]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonFunction] DROP CONSTRAINT [FK_PersonFunction_Person];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonFunction_Function]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonFunction] DROP CONSTRAINT [FK_PersonFunction_Function];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonProject]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Projects] DROP CONSTRAINT [FK_PersonProject];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonTask_Person]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonTask] DROP CONSTRAINT [FK_PersonTask_Person];
GO
IF OBJECT_ID(N'[dbo].[FK_PersonTask_Task]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PersonTask] DROP CONSTRAINT [FK_PersonTask_Task];
GO
IF OBJECT_ID(N'[dbo].[FK_ProjectTask]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Tasks] DROP CONSTRAINT [FK_ProjectTask];
GO
IF OBJECT_ID(N'[dbo].[FK_TaskTaskResource]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Resources_TaskResource] DROP CONSTRAINT [FK_TaskTaskResource];
GO
IF OBJECT_ID(N'[dbo].[FK_ProjectGlobalResource]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Resources_GlobalResource] DROP CONSTRAINT [FK_ProjectGlobalResource];
GO
IF OBJECT_ID(N'[dbo].[FK_TypeUnit]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Types] DROP CONSTRAINT [FK_TypeUnit];
GO
IF OBJECT_ID(N'[dbo].[FK_StoreItemType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StoreItems] DROP CONSTRAINT [FK_StoreItemType];
GO
IF OBJECT_ID(N'[dbo].[FK_ContractorContact]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Contacts] DROP CONSTRAINT [FK_ContractorContact];
GO
IF OBJECT_ID(N'[dbo].[FK_DocumentLocation]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Documents] DROP CONSTRAINT [FK_DocumentLocation];
GO
IF OBJECT_ID(N'[dbo].[FK_DocumentContractor]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Documents] DROP CONSTRAINT [FK_DocumentContractor];
GO
IF OBJECT_ID(N'[dbo].[FK_OperationDocument]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Documents] DROP CONSTRAINT [FK_OperationDocument];
GO
IF OBJECT_ID(N'[dbo].[FK_StoreOperationStoreItem]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Operations_StoreOperation] DROP CONSTRAINT [FK_StoreOperationStoreItem];
GO
IF OBJECT_ID(N'[dbo].[FK_OperationOperation]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Operations] DROP CONSTRAINT [FK_OperationOperation];
GO
IF OBJECT_ID(N'[dbo].[FK_OperationOperationType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Operations] DROP CONSTRAINT [FK_OperationOperationType];
GO
IF OBJECT_ID(N'[dbo].[FK_TaskResource_inherits_Resource]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Resources_TaskResource] DROP CONSTRAINT [FK_TaskResource_inherits_Resource];
GO
IF OBJECT_ID(N'[dbo].[FK_GlobalResource_inherits_Resource]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Resources_GlobalResource] DROP CONSTRAINT [FK_GlobalResource_inherits_Resource];
GO
IF OBJECT_ID(N'[dbo].[FK_StoreOperation_inherits_Operation]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Operations_StoreOperation] DROP CONSTRAINT [FK_StoreOperation_inherits_Operation];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[People]', 'U') IS NOT NULL
    DROP TABLE [dbo].[People];
GO
IF OBJECT_ID(N'[dbo].[Functions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Functions];
GO
IF OBJECT_ID(N'[dbo].[Projects]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Projects];
GO
IF OBJECT_ID(N'[dbo].[Tasks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Tasks];
GO
IF OBJECT_ID(N'[dbo].[Resources]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Resources];
GO
IF OBJECT_ID(N'[dbo].[CashBoxes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CashBoxes];
GO
IF OBJECT_ID(N'[dbo].[StoreItems]', 'U') IS NOT NULL
    DROP TABLE [dbo].[StoreItems];
GO
IF OBJECT_ID(N'[dbo].[Types]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Types];
GO
IF OBJECT_ID(N'[dbo].[Units]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Units];
GO
IF OBJECT_ID(N'[dbo].[Contacts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Contacts];
GO
IF OBJECT_ID(N'[dbo].[Contractors]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Contractors];
GO
IF OBJECT_ID(N'[dbo].[Documents]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Documents];
GO
IF OBJECT_ID(N'[dbo].[Locations]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Locations];
GO
IF OBJECT_ID(N'[dbo].[Operations]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Operations];
GO
IF OBJECT_ID(N'[dbo].[OperationTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OperationTypes];
GO
IF OBJECT_ID(N'[dbo].[Accounts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Accounts];
GO
IF OBJECT_ID(N'[dbo].[Resources_TaskResource]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Resources_TaskResource];
GO
IF OBJECT_ID(N'[dbo].[Resources_GlobalResource]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Resources_GlobalResource];
GO
IF OBJECT_ID(N'[dbo].[Operations_StoreOperation]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Operations_StoreOperation];
GO
IF OBJECT_ID(N'[dbo].[PersonFunction]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonFunction];
GO
IF OBJECT_ID(N'[dbo].[PersonTask]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PersonTask];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'People'
CREATE TABLE [dbo].[People] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Surname] nvarchar(max)  NOT NULL,
    [Phone_Description] nvarchar(max)  NULL,
    [Phone_PhoneNumber] nvarchar(max)  NULL,
    [Email_Description] nvarchar(max)  NULL,
    [Email_mail] nvarchar(max)  NULL,
    [FotoFoto] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Functions'
CREATE TABLE [dbo].[Functions] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Description] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Projects'
CREATE TABLE [dbo].[Projects] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [StartDate] datetime  NOT NULL,
    [EndDate] datetime  NOT NULL,
    [Creator_Id] int  NOT NULL
);
GO

-- Creating table 'Tasks'
CREATE TABLE [dbo].[Tasks] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [StartDate] datetime  NOT NULL,
    [EndDate] datetime  NOT NULL,
    [IsActive] bit  NOT NULL,
    [Project_Id] int  NULL
);
GO

-- Creating table 'Resources'
CREATE TABLE [dbo].[Resources] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Value] int  NOT NULL
);
GO

-- Creating table 'CashBoxes'
CREATE TABLE [dbo].[CashBoxes] (
    [Id] uniqueidentifier  NOT NULL,
    [Quantity] nvarchar(max)  NOT NULL,
    [DisposedQuantity] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'StoreItems'
CREATE TABLE [dbo].[StoreItems] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Quantity] int  NOT NULL,
    [Price] int  NOT NULL,
    [Type_Id] int  NOT NULL
);
GO

-- Creating table 'Types'
CREATE TABLE [dbo].[Types] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Unit_Id] int  NOT NULL
);
GO

-- Creating table 'Units'
CREATE TABLE [dbo].[Units] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ShortCut] nvarchar(max)  NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Contacts'
CREATE TABLE [dbo].[Contacts] (
    [Id] uniqueidentifier  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Surname] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Phone_Description] nvarchar(max)  NULL,
    [Phone_PhoneNumber] nvarchar(max)  NULL,
    [Email_Description] nvarchar(max)  NULL,
    [Email_mail] nvarchar(max)  NULL,
    [Contractor_Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Contractors'
CREATE TABLE [dbo].[Contractors] (
    [Id] uniqueidentifier  NOT NULL,
    [Address_Street] nvarchar(max)  NULL,
    [Address_City] nvarchar(max)  NULL,
    [Address_PostCode] nvarchar(max)  NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Email] nvarchar(max)  NOT NULL,
    [PhoneNumber] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Documents'
CREATE TABLE [dbo].[Documents] (
    [Id] uniqueidentifier  NOT NULL,
    [CreationDate] datetime  NOT NULL,
    [Number] nvarchar(max)  NOT NULL,
    [Location_Id] uniqueidentifier  NOT NULL,
    [Contractor_Id] uniqueidentifier  NOT NULL,
    [OperationDocument_Document_Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Locations'
CREATE TABLE [dbo].[Locations] (
    [Id] uniqueidentifier  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Operations'
CREATE TABLE [dbo].[Operations] (
    [Id] uniqueidentifier  NOT NULL,
    [CreationDate] datetime  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Amount] nvarchar(max)  NOT NULL,
    [OperationOperation_Operation1_Id] uniqueidentifier  NOT NULL,
    [OperationType_Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'OperationTypes'
CREATE TABLE [dbo].[OperationTypes] (
    [Id] uniqueidentifier  NOT NULL,
    [ShortCut] nvarchar(max)  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [IsOutputOperation] bit  NOT NULL
);
GO

-- Creating table 'Accounts'
CREATE TABLE [dbo].[Accounts] (
    [Id] uniqueidentifier  NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Resources_TaskResource'
CREATE TABLE [dbo].[Resources_TaskResource] (
    [Id] int  NOT NULL,
    [Task_Id] int  NOT NULL
);
GO

-- Creating table 'Resources_GlobalResource'
CREATE TABLE [dbo].[Resources_GlobalResource] (
    [Id] int  NOT NULL,
    [Project_Id] int  NOT NULL
);
GO

-- Creating table 'Operations_StoreOperation'
CREATE TABLE [dbo].[Operations_StoreOperation] (
    [Id] uniqueidentifier  NOT NULL,
    [StoreItem_Id] int  NOT NULL
);
GO

-- Creating table 'PersonFunction'
CREATE TABLE [dbo].[PersonFunction] (
    [Person_Id] int  NOT NULL,
    [Functions_Id] int  NOT NULL
);
GO

-- Creating table 'PersonTask'
CREATE TABLE [dbo].[PersonTask] (
    [PeopeInvolved_Id] int  NOT NULL,
    [Tasks_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'People'
ALTER TABLE [dbo].[People]
ADD CONSTRAINT [PK_People]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Functions'
ALTER TABLE [dbo].[Functions]
ADD CONSTRAINT [PK_Functions]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [PK_Projects]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [PK_Tasks]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Resources'
ALTER TABLE [dbo].[Resources]
ADD CONSTRAINT [PK_Resources]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'CashBoxes'
ALTER TABLE [dbo].[CashBoxes]
ADD CONSTRAINT [PK_CashBoxes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'StoreItems'
ALTER TABLE [dbo].[StoreItems]
ADD CONSTRAINT [PK_StoreItems]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Types'
ALTER TABLE [dbo].[Types]
ADD CONSTRAINT [PK_Types]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Units'
ALTER TABLE [dbo].[Units]
ADD CONSTRAINT [PK_Units]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Contacts'
ALTER TABLE [dbo].[Contacts]
ADD CONSTRAINT [PK_Contacts]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Contractors'
ALTER TABLE [dbo].[Contractors]
ADD CONSTRAINT [PK_Contractors]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Documents'
ALTER TABLE [dbo].[Documents]
ADD CONSTRAINT [PK_Documents]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Locations'
ALTER TABLE [dbo].[Locations]
ADD CONSTRAINT [PK_Locations]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Operations'
ALTER TABLE [dbo].[Operations]
ADD CONSTRAINT [PK_Operations]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'OperationTypes'
ALTER TABLE [dbo].[OperationTypes]
ADD CONSTRAINT [PK_OperationTypes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Accounts'
ALTER TABLE [dbo].[Accounts]
ADD CONSTRAINT [PK_Accounts]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Resources_TaskResource'
ALTER TABLE [dbo].[Resources_TaskResource]
ADD CONSTRAINT [PK_Resources_TaskResource]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Resources_GlobalResource'
ALTER TABLE [dbo].[Resources_GlobalResource]
ADD CONSTRAINT [PK_Resources_GlobalResource]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Operations_StoreOperation'
ALTER TABLE [dbo].[Operations_StoreOperation]
ADD CONSTRAINT [PK_Operations_StoreOperation]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Person_Id], [Functions_Id] in table 'PersonFunction'
ALTER TABLE [dbo].[PersonFunction]
ADD CONSTRAINT [PK_PersonFunction]
    PRIMARY KEY NONCLUSTERED ([Person_Id], [Functions_Id] ASC);
GO

-- Creating primary key on [PeopeInvolved_Id], [Tasks_Id] in table 'PersonTask'
ALTER TABLE [dbo].[PersonTask]
ADD CONSTRAINT [PK_PersonTask]
    PRIMARY KEY NONCLUSTERED ([PeopeInvolved_Id], [Tasks_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Person_Id] in table 'PersonFunction'
ALTER TABLE [dbo].[PersonFunction]
ADD CONSTRAINT [FK_PersonFunction_Person]
    FOREIGN KEY ([Person_Id])
    REFERENCES [dbo].[People]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Functions_Id] in table 'PersonFunction'
ALTER TABLE [dbo].[PersonFunction]
ADD CONSTRAINT [FK_PersonFunction_Function]
    FOREIGN KEY ([Functions_Id])
    REFERENCES [dbo].[Functions]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonFunction_Function'
CREATE INDEX [IX_FK_PersonFunction_Function]
ON [dbo].[PersonFunction]
    ([Functions_Id]);
GO

-- Creating foreign key on [Creator_Id] in table 'Projects'
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT [FK_PersonProject]
    FOREIGN KEY ([Creator_Id])
    REFERENCES [dbo].[People]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonProject'
CREATE INDEX [IX_FK_PersonProject]
ON [dbo].[Projects]
    ([Creator_Id]);
GO

-- Creating foreign key on [PeopeInvolved_Id] in table 'PersonTask'
ALTER TABLE [dbo].[PersonTask]
ADD CONSTRAINT [FK_PersonTask_Person]
    FOREIGN KEY ([PeopeInvolved_Id])
    REFERENCES [dbo].[People]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Tasks_Id] in table 'PersonTask'
ALTER TABLE [dbo].[PersonTask]
ADD CONSTRAINT [FK_PersonTask_Task]
    FOREIGN KEY ([Tasks_Id])
    REFERENCES [dbo].[Tasks]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PersonTask_Task'
CREATE INDEX [IX_FK_PersonTask_Task]
ON [dbo].[PersonTask]
    ([Tasks_Id]);
GO

-- Creating foreign key on [Project_Id] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [FK_ProjectTask]
    FOREIGN KEY ([Project_Id])
    REFERENCES [dbo].[Projects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProjectTask'
CREATE INDEX [IX_FK_ProjectTask]
ON [dbo].[Tasks]
    ([Project_Id]);
GO

-- Creating foreign key on [Task_Id] in table 'Resources_TaskResource'
ALTER TABLE [dbo].[Resources_TaskResource]
ADD CONSTRAINT [FK_TaskTaskResource]
    FOREIGN KEY ([Task_Id])
    REFERENCES [dbo].[Tasks]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TaskTaskResource'
CREATE INDEX [IX_FK_TaskTaskResource]
ON [dbo].[Resources_TaskResource]
    ([Task_Id]);
GO

-- Creating foreign key on [Project_Id] in table 'Resources_GlobalResource'
ALTER TABLE [dbo].[Resources_GlobalResource]
ADD CONSTRAINT [FK_ProjectGlobalResource]
    FOREIGN KEY ([Project_Id])
    REFERENCES [dbo].[Projects]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProjectGlobalResource'
CREATE INDEX [IX_FK_ProjectGlobalResource]
ON [dbo].[Resources_GlobalResource]
    ([Project_Id]);
GO

-- Creating foreign key on [Unit_Id] in table 'Types'
ALTER TABLE [dbo].[Types]
ADD CONSTRAINT [FK_TypeUnit]
    FOREIGN KEY ([Unit_Id])
    REFERENCES [dbo].[Units]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TypeUnit'
CREATE INDEX [IX_FK_TypeUnit]
ON [dbo].[Types]
    ([Unit_Id]);
GO

-- Creating foreign key on [Type_Id] in table 'StoreItems'
ALTER TABLE [dbo].[StoreItems]
ADD CONSTRAINT [FK_StoreItemType]
    FOREIGN KEY ([Type_Id])
    REFERENCES [dbo].[Types]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StoreItemType'
CREATE INDEX [IX_FK_StoreItemType]
ON [dbo].[StoreItems]
    ([Type_Id]);
GO

-- Creating foreign key on [Contractor_Id] in table 'Contacts'
ALTER TABLE [dbo].[Contacts]
ADD CONSTRAINT [FK_ContractorContact]
    FOREIGN KEY ([Contractor_Id])
    REFERENCES [dbo].[Contractors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ContractorContact'
CREATE INDEX [IX_FK_ContractorContact]
ON [dbo].[Contacts]
    ([Contractor_Id]);
GO

-- Creating foreign key on [Location_Id] in table 'Documents'
ALTER TABLE [dbo].[Documents]
ADD CONSTRAINT [FK_DocumentLocation]
    FOREIGN KEY ([Location_Id])
    REFERENCES [dbo].[Locations]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DocumentLocation'
CREATE INDEX [IX_FK_DocumentLocation]
ON [dbo].[Documents]
    ([Location_Id]);
GO

-- Creating foreign key on [Contractor_Id] in table 'Documents'
ALTER TABLE [dbo].[Documents]
ADD CONSTRAINT [FK_DocumentContractor]
    FOREIGN KEY ([Contractor_Id])
    REFERENCES [dbo].[Contractors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DocumentContractor'
CREATE INDEX [IX_FK_DocumentContractor]
ON [dbo].[Documents]
    ([Contractor_Id]);
GO

-- Creating foreign key on [OperationDocument_Document_Id] in table 'Documents'
ALTER TABLE [dbo].[Documents]
ADD CONSTRAINT [FK_OperationDocument]
    FOREIGN KEY ([OperationDocument_Document_Id])
    REFERENCES [dbo].[Operations]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OperationDocument'
CREATE INDEX [IX_FK_OperationDocument]
ON [dbo].[Documents]
    ([OperationDocument_Document_Id]);
GO

-- Creating foreign key on [StoreItem_Id] in table 'Operations_StoreOperation'
ALTER TABLE [dbo].[Operations_StoreOperation]
ADD CONSTRAINT [FK_StoreOperationStoreItem]
    FOREIGN KEY ([StoreItem_Id])
    REFERENCES [dbo].[StoreItems]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StoreOperationStoreItem'
CREATE INDEX [IX_FK_StoreOperationStoreItem]
ON [dbo].[Operations_StoreOperation]
    ([StoreItem_Id]);
GO

-- Creating foreign key on [OperationOperation_Operation1_Id] in table 'Operations'
ALTER TABLE [dbo].[Operations]
ADD CONSTRAINT [FK_OperationOperation]
    FOREIGN KEY ([OperationOperation_Operation1_Id])
    REFERENCES [dbo].[Operations]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OperationOperation'
CREATE INDEX [IX_FK_OperationOperation]
ON [dbo].[Operations]
    ([OperationOperation_Operation1_Id]);
GO

-- Creating foreign key on [OperationType_Id] in table 'Operations'
ALTER TABLE [dbo].[Operations]
ADD CONSTRAINT [FK_OperationOperationType]
    FOREIGN KEY ([OperationType_Id])
    REFERENCES [dbo].[OperationTypes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OperationOperationType'
CREATE INDEX [IX_FK_OperationOperationType]
ON [dbo].[Operations]
    ([OperationType_Id]);
GO

-- Creating foreign key on [Id] in table 'Resources_TaskResource'
ALTER TABLE [dbo].[Resources_TaskResource]
ADD CONSTRAINT [FK_TaskResource_inherits_Resource]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Resources]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'Resources_GlobalResource'
ALTER TABLE [dbo].[Resources_GlobalResource]
ADD CONSTRAINT [FK_GlobalResource_inherits_Resource]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Resources]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'Operations_StoreOperation'
ALTER TABLE [dbo].[Operations_StoreOperation]
ADD CONSTRAINT [FK_StoreOperation_inherits_Operation]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Operations]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------